using FastDevTemplate.Service.Base.EventBus;
using FastDevTemplate.Service.Event.MessageObject;
using FastDevTemplate.Service.Interface;
using Microsoft.Extensions.DependencyInjection;

namespace FastDevTemplate.Service.Event;

public static class UserLogEventHandle
{
    public static void UseUserLogEvent(this IServiceCollection service)
    {
        LocalEventBus.Subscribe<UserLogMessage>((msg) =>
        {
            // ILogService userLogService = new LogService(new FastDevTemplateDbContext()); 
            
            var serviceProvider = service.BuildServiceProvider();
            var userLogService = serviceProvider.GetService<ILogService>();
            if (userLogService == null) 
                throw new Exception($" {nameof(UserLogEventHandle)} : Get UserLogService Error.");
            
            userLogService.AddAsync(msg);
        });
    }
}